package com.show.antenna.controller;


import com.alibaba.fastjson.JSON;
import com.show.antenna.bean.*;
import com.show.antenna.service.AntenaService;
import com.show.antenna.service.impl.AntenaServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@RestController
@RequestMapping("antena")
public class AntennaController {
    @Autowired
    AntenaService antenaService;
    @Autowired
    AntenaServiceImpl antenaServiceImpl;

    @Value("${rasterfile.templateLoadPath}")
    public String templateLoadPath;

    @Value("${rasterfile.picLoadPath}")
    public String picLoadPath;

    @RequestMapping("/index")
    public String sayHello() {
        return "index";
    }

    /**
     * 用户登录
     */
    @RequestMapping(value = "/userlogin", method = RequestMethod.POST)
    @ResponseBody
    public List<UserInfoEntity> userLogin(@RequestBody UserInfoEntity userInfoEntity) {
        return antenaService.userLogin(userInfoEntity);
    }

    /**
     * 用户名查找
     */
    @RequestMapping(value = "/searchUser", method = RequestMethod.POST)
    @ResponseBody
    public String searchUser(@RequestBody UserInfoEntity userInfoEntity) {
        return antenaService.searchUser(userInfoEntity);
    }

    /**
     * 用户注册
     */
    @RequestMapping(value = "/userRegister", method = RequestMethod.POST)
    @ResponseBody
    public String userRegister(@RequestBody UserInfoEntity userInfoEntity) {
        return antenaService.userRegister(userInfoEntity);
    }

    /**
     * 获取用户信息
     */
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public List<UserInfoEntity> getUserInfo() {
        return antenaService.getUserInfo();
    }

    /**
     * 修改用户类型
     */
    @RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public String updateUserInfo(@RequestBody UserInfoEntity userInfoEntity) {
        return antenaService.updateUserInfo(userInfoEntity);
    }

    /**
     * 获取影像数据
     */
    @RequestMapping(value = "/getRaster", method = RequestMethod.POST)
    @ResponseBody
    public List<RasterEntity> getRaster() {
        return antenaService.getRaster();
    }

    /**
     * 获取带审批的影像数据
     */
    @RequestMapping(value = "/getRaster2", method = RequestMethod.POST)
    @ResponseBody
    public List<RasterEntity> getRaster2(@RequestBody ApprovalEntity approvalEntity) {
        return antenaService.getRaster2(approvalEntity);
    }

    /**
     * 获取带审批的影像数据 过滤所属属性、所属区域
     */
    @RequestMapping(value = "/getRasterByType", method = RequestMethod.POST)
    @ResponseBody
    public List<RasterEntity> getRasterByType(@RequestBody RasterTypeEntity rasterTypeEntity) {
        return antenaService.getRasterByType(rasterTypeEntity);
    }

    /**
     * 获取带审批的影像数据 过滤所属属性和所属区域
     */
    @RequestMapping(value = "/getRasterByTypeAndArea", method = RequestMethod.POST)
    @ResponseBody
    public List<RasterEntity> getRasterByTypeAndArea(@RequestBody RasterTypeEntity rasterTypeEntity) {
        return antenaService.getRasterByTypeAndArea(rasterTypeEntity);
    }

    /**
     * 获取带审批的影像数据 过滤时间段
     */
    @RequestMapping(value = "/getRasterByTime", method = RequestMethod.POST)
    @ResponseBody
    public List<RasterEntity> getRasterByTime(@RequestBody RasterTypeEntity rasterTypeEntity) {
        return antenaService.getRasterByTime(rasterTypeEntity);
    }

    /**
     * 获取带审批的影像数据 过滤时间段、所属属性和所属区域
     */
    @RequestMapping(value = "/getRasterByTypeAreaTime", method = RequestMethod.POST)
    @ResponseBody
    public List<RasterEntity> getRasterByTypeAreaTime(@RequestBody RasterTypeEntity rasterTypeEntity) {
        return antenaService.getRasterByTypeAreaTime(rasterTypeEntity);
    }

    /**
     * 更新影像审批状态
     */
    @RequestMapping(value = "/updataApprovalInfo", method = RequestMethod.POST)
    @ResponseBody
    public String updataApprovalInfo(@RequestBody ApprovalEntity approvalEntity) {
        return antenaService.updataApprovalInfo(approvalEntity);
    }

    /**
     * 发起申请
     */
    @RequestMapping(value = "/addApprovalInfo", method = RequestMethod.POST)
    @ResponseBody
    public String addApprovalInfo(@RequestBody ApprovalEntity approvalEntity) {
        return antenaService.addApprovalInfo(approvalEntity);
    }

    /**
     * 获取影像审批
     */
    @RequestMapping(value = "/getApprovalInfo", method = RequestMethod.POST)
    @ResponseBody
    public List<ApprovalEntity> getApprovalInfo() {
        return antenaService.getApprovalInfo();
    }

    /**
     * 获取影像审批
     */
    @RequestMapping(value = "/addRaster", method = RequestMethod.POST)
    @ResponseBody
    public String addRaster(@RequestBody RasterEntity rasterEntity) {
        return antenaService.addRaster(rasterEntity);
    }


    /**
     * 上传文件
     *
     * @return 对象列表
     */
    @RequestMapping("/uploadFile")
    @ResponseBody
    public String uploadFile(MultipartFile file, HttpServletRequest request, HttpSession session) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String resultInfo = "false";
        try {
            String originalFilename = file.getOriginalFilename();
            if (originalFilename != null && originalFilename.length() > 0) {
                //获取后缀名
                String sufixName = originalFilename.substring(originalFilename.indexOf("."));
                //重命名
                String newName = UUID.randomUUID() + sufixName;
                //设置文件上传的目录
                String picPath = templateLoadPath;
                File filePath = new File(picPath);
                //如果保存文件的地址不存在，就先创建目录
                if (!filePath.exists()) {
                    filePath.mkdirs();
                }

                //新文件
                File newFile = new File(picPath + newName);
                // 将内存中的数据写入磁盘
                file.transferTo(newFile);
                resultMap.put("code", "ok");
                resultMap.put("filename", newName);
                resultInfo = JSON.toJSONString(resultMap);
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
            resultMap.put("code", "false");
            resultInfo = JSON.toJSONString(resultMap);
            return resultInfo;
        } catch (IOException e) {
            e.printStackTrace();
            resultMap.put("code", "false");
            resultInfo = JSON.toJSONString(resultMap);
            return resultInfo;
        }
        return resultInfo;
    }


    /**
     * 根据ID 更新影像文件地址
     */
    @RequestMapping(value = "/updateRasterPath", method = RequestMethod.POST)
    @ResponseBody
    public String updateRasterPath(@RequestBody RasterEntity rasterEntity) {
        return antenaService.updateRasterPath(rasterEntity);
    }


    /**
     * 上传图片
     *
     * @return 对象列表
     */
    @RequestMapping("/uploadPic")
    @ResponseBody
    public String uploadPic(MultipartFile file, HttpServletRequest request, HttpSession session) {
        Map<String, Object> resultPicMap = new HashMap<String, Object>();
        String resultInfo = "false";
        try {
            String originalFilename = file.getOriginalFilename();
            if (originalFilename != null && originalFilename.length() > 0) {
                //获取后缀名
                String sufixName = originalFilename.substring(originalFilename.indexOf("."));
                //重命名
                String newName = UUID.randomUUID() + sufixName;
                //设置图片上传的目录
                String picPath = picLoadPath;
                File filePath = new File(picPath);
                //如果保存文件的地址不存在，就先创建目录
                if (!filePath.exists()) {
                    filePath.mkdirs();
                }

                //新文件
                File newFile = new File(picPath + newName);
                // 将内存中的数据写入磁盘
                file.transferTo(newFile);
                resultPicMap.put("code", "ok");
                resultPicMap.put("filename", newName);
                resultInfo = JSON.toJSONString(resultPicMap);
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
            resultPicMap.put("code", "false");
            resultInfo = JSON.toJSONString(resultPicMap);
            return resultInfo;
        } catch (IOException e) {
            e.printStackTrace();
            resultPicMap.put("code", "false");
            resultInfo = JSON.toJSONString(resultPicMap);
            return resultInfo;
        }
        return resultInfo;
    }

    /**
     * 根据ID 更新例图文件地址
     */
    @RequestMapping(value = "/updatePicPath", method = RequestMethod.POST)
    @ResponseBody
    public String updatePicPath(@RequestBody RasterEntity rasterEntity) {
        return antenaService.updatePicPath(rasterEntity);
    }


    /**
     * 根据ID 下载影像文件
     */
    @RequestMapping(value = "/downloadfile")
    @ResponseBody
    public String downloadfile(HttpServletRequest request, HttpServletResponse response) {
        String downfilename = request.getQueryString().split("=")[1];
        String fileName = downfilename;// 文件名
        if (fileName != null) {
            //设置文件路径
            String downFile = templateLoadPath + downfilename;
            File file = new File(downFile);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    return "下载成功";
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return "下载失败";
    }


}
